/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_05_additional_robustness.do
Date: October 2022

Description: Reproduces Table 5: Contribution of firm-embedded productivity, additional robustness.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"
	
*Log
cap log close
log using "${clogs}/table_05_additional_robustness.log",replace
*-------------------------------------------------------------------------------

*Notice that aggregates are only for countries with DA dummies in more than 10 sectors. A DA for a sector is calculated if at least 3 affiliates operate in that coutry-sector pair
*minimum number of sectors allowed per year-isocode*
local nn=10 

foreach rsample in "tax_ratio" "BTT" "gdpdiff" "nogravity" "exclnomark" "ctte" "aboveage10" {
display "`rsample'"
clear all  
use "${data}/estimates_sec_naics_sales_s1_base_woparent_`rsample'.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${data}/estimates_agg_naics_sales_s1_base_woparent_`rsample'.dta", clear
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
tempfile file_`rsample'
save `file_`rsample'', replace 
}

foreach rsample in naics4 {
display "`rsample'"
clear all  
use "${data}/estimates_sec_`rsample'_sales_s1_base_woparent.dta", clear
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 

use "${data}/estimates_agg_`rsample'_sales_s1_base_woparent.dta", clear
merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
tempfile file_`rsample'
save `file_`rsample'', replace 
}


*Merging the different samples
*-----------------------------------------------------------------
clear all
use year isocode type D_A using "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear
rename D_A D_A_baseline

foreach rsample in tax_ratio BTT gdpdiff nogravity exclnomark ctte aboveage10 naics4 {
display "`rsample'"
merge 1:1 year isocode type using `file_`rsample'', keepusing(D_A*)
rename D_A D_A_`rsample'
drop _merge
}

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "P"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A_baseline D_A_tax_ratio D_A_BTT D_A_gdpdiff D_A_nogravity D_A_exclnomark D_A_ctte D_A_aboveage10 D_A_naics4  {
gen `var'_phi=(-1)*`var'*(0.2)
}



*------------------------------------------------------------------------------
*DA dummies (baseline)
*------------------------------------------------------------------------------
local LHS1 D_A_baseline_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c0: display %-03.2fc round(b[1,1],0.01)
global se0: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c0
display $se0


*------------------------------------------------------------------------------
*DA dummies controlling for bilateral MNE-specific tax rates 
*------------------------------------------------------------------------------
local LHS1 D_A_tax_ratio_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c1
display $se1


*------------------------------------------------------------------------------
*DA dummies controlling for Bilateral Tax Treaties
*------------------------------------------------------------------------------
local LHS1  D_A_BTT_phi 
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c2
display $se2


*------------------------------------------------------------------------------
*DA dummies controlling for differences in GDP per-worker between source and host country
*------------------------------------------------------------------------------
local LHS1 D_A_gdpdiff_phi 
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c3: display %-03.2fc round(b[1,1],0.01)
global se3: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c3
display $se3


*------------------------------------------------------------------------------
*DA dummies excluding gravity variables 
*------------------------------------------------------------------------------
local LHS1 D_A_nogravity_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c4: display %-03.2fc round(b[1,1],0.01)
global se4: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c4
display $se4


*------------------------------------------------------------------------------
*DA dummies excluding Real Estate, Health, and Education
*------------------------------------------------------------------------------
local LHS1 D_A_exclnomark_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c5: display %-03.2fc round(b[1,1],0.01)
global se5: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c5
display $se5


*------------------------------------------------------------------------------
*DA dummies excluding MNEs that do not appear in ORBIS every year between 2010-2016 (constant sample)
*------------------------------------------------------------------------------
local LHS1 D_A_ctte_phi
local RHS ln_`grvar0'

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c6: display %-03.2fc round(b[1,1],0.01)
global se6: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c6
display $se6


*------------------------------------------------------------------------------
*DA dummies excluding MNE affiliates incorporated after 2006
*------------------------------------------------------------------------------
local LHS1 D_A_aboveage10_phi
local RHS ln_`grvar0'

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c7: display %-03.2fc round(b[1,1],0.01)
global se7: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c7
display $se7


*------------------------------------------------------------------------------
*DA dummies for aggregation at 4-digit NAICS industries 
*------------------------------------------------------------------------------
local LHS1 D_A_naics4_phi
local RHS ln_`grvar0'
	
reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c8: display %-03.2fc round(b[1,1],0.01)
global se8: display %-03.2fc round(sqrt(V[1,1]),0.01)
display $c8
display $se8
	    

		

*------------------------------------------------------------------------------
*Generate Table 5
*------------------------------------------------------------------------------
clear all
set obs 9

gen model=""
gen phi_share=. 
gen phi_share_se=. 


replace model="Baseline" if _n==1
replace phi_share=$c0 if _n==1
replace phi_share_se=$se0 if _n==1

replace model="Controlling for tax_ratio" if _n==2
replace phi_share=$c1 if _n==2
replace phi_share_se=$se1 if _n==2

replace model="Controlling for BTT" if _n==3
replace phi_share=$c2 if _n==3
replace phi_share_se=$se2 if _n==3

replace model="Controlling for GDP per-worker diff" if _n==4
replace phi_share=$c3 if _n==4
replace phi_share_se=$se3 if _n==4

replace model="Excluding gravity var " if _n==5
replace phi_share=$c4 if _n==5
replace phi_share_se=$se4 if _n==5

replace model="Excluding Real Estate, Health, and Education" if _n==6
replace phi_share=$c5 if _n==6
replace phi_share_se=$se5 if _n==6

replace model="Excluding MNEs (not every year)" if _n==7
replace phi_share=$c6 if _n==7
replace phi_share_se=$se6 if _n==7

replace model="Excluding MNE affiliates incorporated after 2006" if _n==8
replace phi_share=$c7 if _n==8
replace phi_share_se=$se7 if _n==8

replace model="Aggregation at 4-digit NAICS industries " if _n==9
replace phi_share=$c8 if _n==9
replace phi_share_se=$se8 if _n==9

export delimited using "${rmain}/table_05_additional_robustness.csv", datafmt replace

log close


